<?php
// @formatter:off
/**
 * @file 5.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: 5.php 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/install/steps/5.php $
 */
// @formatter:on

if(defined('__INSTALLER__')) {
	$conn = WCConnection::Instance();
	$connCode = md5(__FILE__);
	$isOkToContinue = false;

	$dummy = new WCUser();

	/*
	 * Clearing fake main users.
	 */
	$query = "delete from     {$wcDatabaseAccess['prefix']}users\n";
	$query .= "where           (usr_username = '".WC_USER_ROOT_NAME."' and usr_id <> '".WC_USER_ROOT_ID."')\n";
	$query .= " or             (usr_username = '".WC_USER_CRON_NAME."' and usr_id <> '".WC_USER_CRON_ID."')\n";
	$conn->exec($query);

	$root = new WCUser();
	$cron = new WCUser();

	$hasRoot = @$root->load(WC_USER_ROOT_ID);
	$hasCron = @$cron->load(WC_USER_CRON_ID);

	if($hasRoot && $hasCron) {
		if($hasRoot && $root->username() != WC_USER_ROOT_NAME) {
			$isOkToContinue = false;
		} elseif($hasCron && $cron->username() != WC_USER_CRON_NAME) {
			$isOkToContinue = false;
		} else {
			$wcUserDataHolder = WCUserDataHolder::Instance();
			$root->data()->email = $wcSite["email"];

			echo "\t\t\t\t<p>Basic users created:<ul>\n";
			echo "\t\t\t\t\t<li>'[OK:".WC_USER_ROOT_NAME."]' (default password: '".WC_USER_ROOT_NAME."')</li>\n";
			echo "\t\t\t\t\t<li>'[OK:".WC_USER_CRON_NAME."]' (default password: '".WC_USER_CRON_NAME."')</li>\n";
			echo "\t\t\t\t</ul></p>\n";
			$isOkToContinue = true;
			$isOkToReload = true;
		}
	}
	if(!$isOkToContinue) {
		echo "\t\t\t\t<p>This is step {$wciStepCheck}. Here, basic users will be automatically created:</p>\n";
		echo "\t\t\t\t<p><ul>\n";
		if(!$hasRoot) {
			echo "\t\t\t\t\t<li>Creating user '".WC_USER_ROOT_NAME."' (Password: '".WC_USER_ROOT_NAME."')</li>\n";
			$query = "insert\n";
			$query .= "        into {$wcDatabaseAccess['prefix']}users (\n";
			$query .= "                usr_id, usr_username, usr_password, usr_web_access, usr_sys_access)\n";
			$query .= "        values (:id, :name, md5(:name), 'Y', 'Y')\n";
			$stmt = $conn->prepare("{$connCode}[2]", $query);
			$stmt->execute(array(
				":id" => WC_USER_ROOT_ID,
				":name" => WC_USER_ROOT_NAME
			));
		} elseif($root->username() != WC_USER_ROOT_NAME || $cron->profileCode() != "ROOT") {
			echo "\t\t\t\t\t<li>Updating username for '".WC_USER_ROOT_NAME."'</li>\n";
			$query = "update  {$wcDatabaseAccess['prefix']}users\n";
			$query .= "set     usr_username   = :name,\n";
			$query .= "        usr_web_access = 'Y',\n";
			$query .= "        usr_sys_access = 'Y'\n";
			$query .= "where   usr_id = :id\n";
			$stmt = $conn->prepare("{$connCode}[3]", $query);
			$stmt->execute(array(
				":id" => WC_USER_ROOT_ID,
				":name" => WC_USER_ROOT_NAME
			));
		} else {
			echo "\t\t\t\t<li>[OK:User '".WC_USER_ROOT_NAME."' created.]</li>\n";
		}
		if(!$hasCron) {
			echo "\t\t\t\t\t<li>Creating user '".WC_USER_CRON_NAME."' (Password: '".WC_USER_CRON_NAME."')</li>\n";
			$query = "insert\n";
			$query .= "        into {$wcDatabaseAccess['prefix']}users (\n";
			$query .= "                usr_id, usr_username, usr_password, usr_web_access, usr_sys_access)\n";
			$query .= "        values (:id, :name, md5(:name), 'N', 'Y')\n";
			$stmt = $conn->prepare("{$connCode}[4]", $query);
			$stmt->execute(array(
				":id" => WC_USER_CRON_ID,
				":name" => WC_USER_CRON_NAME
			));
		} elseif($cron->username() != WC_USER_CRON_NAME || $cron->profileCode() != "ROOT") {
			echo "\t\t\t\t\t<li>Updating username for '".WC_USER_CRON_NAME."'</li>\n";
			$query = "update  {$wcDatabaseAccess['prefix']}users\n";
			$query .= "set     usr_username   = :name,\n";
			$query .= "        usr_web_access = 'N',\n";
			$query .= "        usr_sys_access = 'Y'\n";
			$query .= "where   usr_id = :id\n";
			$stmt = $conn->prepare("{$connCode}[5]", $query);
			$stmt->execute(array(
				":id" => WC_USER_CRON_ID,
				":name" => WC_USER_CRON_NAME
			));
		} else {
			echo "\t\t\t\t<li>[OK:User '".WC_USER_CRON_NAME."' created.]</li>\n";
		}
		echo "\t\t\t\t</ul></p>\n";
		$isOkToReload = true;
	}
}
?>